<?php

namespace App\Business\Users;

use App\Business\BusBase;
use App\Business\DataLog\DataLogin;
use App\Business\ReturnResult;
use App\Extend\UserIDInfo;
use App\Jobs\LogQueue;
use App\Models\users;
use Illuminate\Support\Facades\Auth;

/**
 * Class Login 登录
 * @package App\Business\Users
 */
class Login extends BusBase
{
    use UserIDInfo;
    /**
     * @var 用户名
     */
    public $userName;

    /**
     * @var 密码
     */
    public $password;

    protected function Handle()
    {
        return $this->LoginFromUserName();

    }


    public function GetBusinessName()
    {
        return "登录";
    }





    /**
     * 用户名和密码登录
     * @return ReturnResult
     */
    protected function LoginFromUserName()
    {

        $userName=strtolower($this->userName);

        $password=$this->password;


       /* $user= users::where("userName","=",$userName)
            ->where("password","=",$password)
            ->first();*/

        if(Auth::attempt(['userName' => $userName, 'password' => $password])) {

            $this->SetUserIDFromUser();

           //登录成功，记录到登录日志
            dispatch( new LogQueue(new DataLogin()));

            return ReturnResult::Success(["token"=>request()->session()->token()]);
        }else
        {
            return ReturnResult::Error(null,"用户名或密码错误,或者未注册");
        }

    }



}